# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import pymysql

from myspider import settings
from myspider.items import MovieItem


class MyspiderPipeline:

    def __init__(self):
        self.conn = None
        self.cursor = None

    def open_spider(self, spider):
        config = settings.MYSQL_CONFIG
        self.conn = pymysql.connect(**config)
        self.cursor = self.conn.cursor()

    def close_spider(self, spider):
        self.conn.close()

    def process_item(self, item, spider):
        """处理蜘蛛程序返回的数据项"""
        # if type(item) == MovieItem:
        #     hasher = hashlib.md5()
        #     hasher.update(str(item['title']).encode())
        #     hasher.update(str(item['rating']).encode())
        #     item['digest'] = hasher.hexdigest()
        name, price = item['name'], item['price']
        self.cursor.execute(
            'insert into tb_goods (`goods_name`, `goods_price`) values (%s, %s)',
            (name, price)
        )
        return item
